/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { checkOpened, changeOpenStatus, getLoginToken } from "@/services/workOrder";
import { message } from "antd";

export default {
  namespace: "workOrder",

  state: {
    openStatus: false,
  },

  effects: {
    *checkOpened({ payload }, { call, put }) {
      const response = yield call(checkOpened, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { status = 0 } = data;

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            openStatus: status == 1,
          },
        });
      } else {
        message.error(msg);
      }
    },
    *changeOpenStatus({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(changeOpenStatus, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { registered = 0 } = data;

      if (code == 0) {
        __dva_resolve(registered == 1);
      } else {
        __dva_reject(response);
        message.error(msg);
      }
    },
    *getLoginToken({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getLoginToken, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { token } = data;

      if (code == 0) {
        __dva_resolve(token);
      } else {
        __dva_reject(response);
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
